{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1149be8c-5fa1-4a9e-b112-75b39fd920ee",
   "metadata": {},
   "source": [
    "## Configuring `Chapyter`"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "25297bbc-e8bb-496a-ac75-740bcf333344",
   "metadata": {},
   "source": [
    "`Chapyter` can be easily configured to use different APIs and different models. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aadfe524",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext chapyter"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e78776ff-d5d3-413e-b202-28e718063cf1",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### Method 1: In-place configuration with `%chapyter` "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4184d38a",
   "metadata": {},
   "source": [
    "\n",
    "You can use the `%chapyter` magic to display and modify configurations of Chapyter in the notebook.\n",
    "\n",
    "👍: You can easily change the configurations in the notebook.\n",
    "\n",
    "👎: You need to do this every time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dd5da64c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapyter(Magics) options\n",
      "----------------------\n",
      "Chapyter.azure_openai_api_base=<Unicode>\n",
      "    The base URL for Azure API. Can be left empty if not using Azure.\n",
      "    Current: ''\n",
      "Chapyter.azure_openai_api_key=<Unicode>\n",
      "    The API key used for Azure API queries. By default this will be read from\n",
      "    the `AZURE_OPENAI_API_KEY` environment variable. Can be left empty if not\n",
      "    using Azure.\n",
      "    Current: ''\n",
      "Chapyter.azure_openai_api_version=<Unicode>\n",
      "    The version of Azure API being used. Can be left empty if not using Azure.\n",
      "    Current: ''\n",
      "Chapyter.azure_openai_default_deployment_id=<Unicode>\n",
      "    The default deployment id for Azure API. Different from OpenAI API, Azure\n",
      "    API requires a deployment id to be specified. Can be left empty if not using\n",
      "    Azure.\n",
      "    Current: ''\n",
      "Chapyter.azure_openai_default_model=<Unicode>\n",
      "    The default model used for Azure API queries. Can be overridden by the\n",
      "    --model flag.\n",
      "    Current: ''\n",
      "Chapyter.default_api_type=<Unicode>\n",
      "    The default type of api that will be used to query the models. \n",
      "    Currently we only support the following:\n",
      "        - openai\n",
      "        - azure\n",
      "    Will add more soon.\n",
      "    Current: 'test-value'\n",
      "Chapyter.openai_api_key=<Unicode>\n",
      "    The API key used for OpenAI API queries. By default this will be read from\n",
      "    the `OPENAI_API_KEY` environment variable. Can be left empty if not using\n",
      "    OpenAI.\n",
      "    Current: None\n",
      "Chapyter.openai_api_org=<Unicode>\n",
      "    The organization ID for OpenAI API. By default this will be read from the\n",
      "    `OPENAI_ORGANIZATION` environment variable. Can be left empty if not using\n",
      "    OpenAI.\n",
      "    Current: None\n",
      "Chapyter.openai_default_model=<Unicode>\n",
      "    The default model that will be used to query the OpenAI API. This can be\n",
      "    overridden by the `--model` flag.\n",
      "    Current: 'gpt-4'\n"
     ]
    }
   ],
   "source": [
    "%chapyter # See all configurations "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e935455d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test-value'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%chapyter default_api_type # See a specific configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4a219e0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'azure'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%chapyter default_api_type='azure' # See a specific configuration\n",
    "%chapyter default_api_type"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "791d095a",
   "metadata": {},
   "source": [
    "#### [Optional] Another alternative way using `%config`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "08bb7240",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'openai'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%config Chapyter.default_api_type=\"openai\"\n",
    "%chapyter default_api_type"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5758700f",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### Method 2: Using `ipython_config.py`"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "fadf7c11",
   "metadata": {},
   "source": [
    "\n",
    "Append the following code to your `ipython_config.py` file and restart the notebook; it will change the configurations of Chapyter accordingly without needing to manually change the configurations in the notebook. \n",
    "\n",
    "\n",
    "```python\n",
    "c.Chapyter.default_api_type = \"test-value\"\n",
    "# You can do this for other configurations as well in a similar format: c.Chapyter.<config_name> = \"XXX\"\n",
    "```\n",
    "\n",
    "Tips:\n",
    "1. Please note the capitalization of `Chapyter` in this case. \n",
    "2. The `ipython_config.py` can be found at `~/.ipython/profile_default/ipython_config.py`; if it does not exist, you can create one by running the bash command `ipython profile create`. \n",
    "3. You might need to restart the (jupyter) notebook and the change will take effect. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b96d2bd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test-value'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%chapyter default_api_type "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a6e80e06",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### Method 3: using environment variables or `.env` files "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3a848ec0",
   "metadata": {},
   "source": [
    "For sensitive information such as API keys, besides storing them in the notebook via the `%chapyter` command or configuring in the `ipython_config.py` file, Chapyter also allows you to use environment variables or `.env` files to store them. \n",
    "\n",
    "The resolution order is:\n",
    "1. In-notebook configurations via `%chapyter`\n",
    "2. Configurations in the `ipython_config.py` file\n",
    "3. Environment variables\n",
    "4. The `.env` file. "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9fb66865",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### Use case 1: Configuring to use Azure OpenAI API"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5c4e229e",
   "metadata": {},
   "source": [
    "#### Method 1: In-place configuration with `%chapyter` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1d2f2404",
   "metadata": {},
   "outputs": [],
   "source": [
    "%chapyter default_api_type=\"azure\"\n",
    "%chapyter azure_openai_default_deployment_id=\"gpt-3.5-turbo\"\n",
    "%chapyter azure_openai_default_model=\"gpt-3.5-turbo\"\n",
    "%chapyter azure_openai_api_key=\"<your-azure-openai-api-key>\"\n",
    "%chapyter azure_openai_api_base=\"https://<your-azure-openai-api-base>.openai.azure.com/\"\n",
    "%chapyter azure_openai_api_version=\"2023-03-15-preview\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2bc19b43",
   "metadata": {},
   "source": [
    "#### Method 2: Use the `ipython_config.py` file"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ad2a2cb1",
   "metadata": {},
   "source": [
    "Append the following code to your `ipython_config.py` file and restart the notebook:\n",
    "```python\n",
    "c.Chapyter.default_api_type=\"azure\"\n",
    "c.Chapyter.azure_openai_default_deployment_id=\"gpt-3.5-turbo\"\n",
    "c.Chapyter.azure_openai_default_model=\"gpt-3.5-turbo\"\n",
    "c.Chapyter.azure_openai_api_key=\"<your-azure-openai-api-key>\"\n",
    "c.Chapyter.azure_openai_api_base=\"https://<your-azure-openai-api-base>.openai.azure.com/\"\n",
    "c.Chapyter.azure_openai_api_version=\"2023-03-15-preview\"\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3849bdff",
   "metadata": {},
   "source": [
    "#### Method 3: Use the `ipython_config.py` and `.env` file"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "17add90a",
   "metadata": {},
   "source": [
    "Append the following code to your `ipython_config.py` file and restart the notebook:\n",
    "```python\n",
    "c.Chapyter.default_api_type=\"azure\"\n",
    "c.Chapyter.azure_openai_default_deployment_id=\"gpt-3.5-turbo\"\n",
    "c.Chapyter.azure_openai_default_model=\"gpt-3.5-turbo\"\n",
    "c.Chapyter.azure_openai_api_base=\"https://<your-azure-openai-api-base>.openai.azure.com/\"\n",
    "c.Chapyter.azure_openai_api_version=\"2023-03-15-preview\"\n",
    "```\n",
    "Add this to the `.env` file in the same directory as the notebook:\n",
    "```bash\n",
    "AZURE_OPENAI_API_KEY=\"<your-azure-openai-api-key>\"\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "051c0dc1",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### Use case 2: Configuring to use the gpt-3.5-turbo model by default"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c633c29d",
   "metadata": {},
   "source": [
    "#### Method 1: In-place configuration with `%chapyter` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5a0da62b",
   "metadata": {},
   "outputs": [],
   "source": [
    "%chapyter openai_default_model=\"gpt-3.5-turbo\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3f506101",
   "metadata": {},
   "source": [
    "#### Method 2: Use the `ipython_config.py` file "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "71707967",
   "metadata": {},
   "source": [
    "Append the following code to your `ipython_config.py` file and restart the notebook:\n",
    "```python\n",
    "c.Chapyter.openai_default_model=\"gpt-3.5-turbo\"\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "03c142b7",
   "metadata": {},
   "source": [
    "### Use case 3: No need to write `%load_ext chapyter` everytime"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6e19d65e",
   "metadata": {},
   "source": [
    "Simply modifying or adding the following line to the `ipython_config.py` file and restart the notebook: \n",
    "\n",
    "```python\n",
    "c.InteractiveShellApp.extensions = [\"chapyter\"]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "80deeae6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test-value'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Note the run id\n",
    "%chapyter default_api_type"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "extension",
   "language": "python",
   "name": "extension"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
